package com.example.administrator.easy.view;

import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.ListView;
import android.widget.Toast;

import com.example.administrator.easy.R;
import com.example.administrator.easy.adapter.GuangRqFenLvAdapter;
import com.example.administrator.easy.adapter.GuangRqStarLvAdapter;
import com.example.administrator.easy.config.NetConfig;
import com.example.administrator.easy.model.GuangRqFenItems;
import com.example.administrator.easy.model.GuangRqFenResult;
import com.example.administrator.easy.model.GuangRqFenRoot;
import com.example.administrator.easy.model.GuangRqRoot;
import com.example.administrator.easy.model.GuangRqStarItems;
import com.example.administrator.easy.model.GuangRqStarResult;
import com.google.gson.Gson;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.ArrayList;

/**
 * Created by Administrator on 2016/11/14.
 */

public class GuangRqFenFragment extends Fragment implements AbsListView.OnScrollListener{
//
    private SwipeRefreshLayout swipeRefreshLayout;
    private ListView lv;
    private ArrayList<GuangRqFenItems> fList;//第一个布局
    private ArrayList<GuangRqFenResult> ffList;//第一个布局
    private GuangRqFenResult gre;
    private ArrayList<GuangRqFenItems> list;//总的

    private GuangRqFenLvAdapter adapter;
    private int curPage = 1;
    private int count=4;
    private boolean isLoad;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_guang_rq_fen,null);
        initView(view);
        initData();
        setListener();
        setData();
        return view;
    }
    private void setData() {
        //
        x.http().get(new RequestParams(NetConfig.FEN+curPage+"/20"), new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
               // Log.e("Mainfen",result);
                GuangRqFenRoot gr = new Gson().fromJson(result,GuangRqFenRoot.class);
               // Log.e("Mainfen",gr.getResult().getItems().size()+"");//20
                fList.add(gr.getResult().getItems().get(0));
               // Log.e("Mainfen",gr.getResult().getItems().get(0)+"");
                fList.add(gr.getResult().getItems().get(1));
                fList.add(gr.getResult().getItems().get(2));
              //  Log.e("Mainfen",fList.size()+"");
                // ffList.set(0,fList);
                gre = new GuangRqFenResult();
                gre.setItems(fList);
                ffList.add(gre);//把集合放到。。
              //  Log.e("Mainfen",ffList+"");

                for(int i=3;i<gr.getResult().getItems().size();i++){
                    GuangRqFenItems items = gr.getResult().getItems().get(i);
                    items.setId(""+count);
                    count++;
                    list.add(items);
                }
             //   Log.e("Mainfen",list+"");
                adapter.notifyDataSetChanged();

            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
             //   Log.e("Mainfen","onerror-----");
            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }

    private void setListener() {
        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                //在此执行刷新的操作
                //重新加载数据
                curPage=1;
                setData();

            }
        });
        lv.setOnScrollListener(this);
    }

    private void initData() {
        fList = new ArrayList<>();
        ffList = new ArrayList<>();
        list = new ArrayList<>();
        adapter = new GuangRqFenLvAdapter(getActivity(),list,ffList);
        lv.setAdapter(adapter);
    }

    private void initView(View view) {
        swipeRefreshLayout = (SwipeRefreshLayout) view.findViewById(R.id.swip);
        //设置刷新的颜色
        swipeRefreshLayout.setColorSchemeColors(Color.rgb(230,57,118));
        lv = (ListView) view.findViewById(R.id.guang_rq_star_lv);
    }

    //滑动监听
    @Override
    public void onScrollStateChanged(AbsListView view, int scrollState) {
        if(isLoad&&SCROLL_STATE_IDLE==scrollState){
            if(curPage<5000){
                Toast.makeText(getActivity(),"正在加载",Toast.LENGTH_LONG).show();
                curPage++;
                setData();
            }else{
                Toast.makeText(getActivity(),"已经是全部了",Toast.LENGTH_LONG).show();
            }

        }
    }

    @Override
    public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
        if(firstVisibleItem+visibleItemCount==totalItemCount){
            isLoad = true;
        }
    }

}
